//
//  BaseViewController.m
//
//
//  Created by Fidetro on 16/9/22.
//  Copyright © 2016年 Fidetro. All rights reserved.
//

#import "DTCBaseViewController.h"
#import "UIButton+ImageTitleSpacing.h"

#define SystemVersionIOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 ? YES : NO)
#ifdef SystemVersionIOS7
#define BASE_TEXTSIZE(text, font) ([text length] > 0 ? [text sizeWithAttributes:@{NSFontAttributeName:font}] : CGSizeZero)
#else
#define BASE_TEXTSIZE(text, font) ([text length] > 0 ? [text sizeWithFont:font] : CGSizeZero)
#endif

#define DTCItemWidth 44
#define DTCItemHeight 44

@interface DTCBaseViewController ()

@end

@implementation DTCBaseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 配置导航栏
    [self configNavigationBar];
    
    [self refreshBaseControlValue];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAllBG) name:ZHYChangeAllBGNotification object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc
{
    NSLog(@"\n\n dealloc : %@  \n\n     \n",[self class]);
}

#pragma mark -
#pragma mark - UI配置部分

/** 配置导航栏 */
- (void)configNavigationBar {
    
    // 隐藏系统导航栏
    [self.navigationController.navigationBar setHidden:YES];
    
    // 添加自定义导航栏
    self.navigationBar = [DTCNavigationBarView getNavigationView:self.view];
    
    [self setBackgroundImage:@""];
    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg"]];
}

- (void)changeAllBG {
    [self setBackgroundImage:@""];
}

/** 隐藏系统导航栏 */
- (void)hideNavigationBar {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
}

/** 隐藏导航栏的下划线 */
- (void)hideNavigationBottomLine{
    [self.navigationBar setLineViewHidden:YES];
}

- (void)setBackgroundImage:(NSString *)imageName {
    self.view.layer.contents = (id)[UIImage imageNamed:imageName].CGImage;
}



/**
 状态栏变白 要设置info.plist View controller-based status bar appearance  为NO
 */
-(UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleDefault;
    
}

#pragma mark - 设置NavigationTitle
- (void)setNavigationTitle:(NSString *)title titleColor:(UIColor *)titleColor
{
    [self.navigationBar setTitle:title textColor:titleColor];
}

- (void)setNavigationBarTitle:(NSString *)title
{
    [self setNavigationTitle:title titleColor:[UIColor whiteColor]];
}
#pragma mark - 左侧按钮设置
- (void)setLeftButtonTitle:(NSString *)title{
    [self.leftButton setTitle:title forState:UIControlStateNormal];
    [self.leftButton setImage:[UIImage imageNamed:@"nav_icon_back"] forState:UIControlStateNormal];
    [self.leftButton sizeToFit];
    [self.leftButton layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:0];
    [self.navigationBar setLeftBarButtonItem:self.leftButton];
}

- (void)setLeftButtonImage:(UIImage *)image{
    [self.leftButton setTitle:@"" forState:UIControlStateNormal];
    [self.leftButton setImage:image forState:UIControlStateNormal];
    [self.leftButton setFrame:CGRectMake(0, 0, image.size.width + 5, image.size.height + 5)];
    [self.navigationBar setLeftBarButtonItem:self.leftButton];
}
#pragma mark - 右侧按钮设置
- (void)setRightButtonTitle:(NSString *)title
{
    [self.rightButton setTitle:title forState:UIControlStateNormal];
    [self.rightButton sizeToFit];
    [self.navigationBar setRightBarButtonItem:self.rightButton];
}

- (void)setRightButtonTitleColor:(UIColor *)titleColor {
    [self.rightButton setTitleColor:titleColor forState:UIControlStateNormal];
}

- (void)setRightButtonImage:(UIImage *)image
{
    [self.rightButton setTitle:@"" forState:UIControlStateNormal];
    [self.rightButton setImage:image forState:UIControlStateNormal];
    [self.rightButton sizeToFit];
    [self.navigationBar setRightBarButtonItem:self.rightButton];
}
#pragma mark - --------------------------touch Event--------------------------
- (void)onLeftButtonClick:(id)sender
{
    if ([self.navigationController.visibleViewController isMemberOfClass:[UITabBarController class]]) {
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onRightButtonClick:(id)sender
{
    
}

-(void)touchesEnded:(NSSet<UITouch*> *)touches withEvent:(UIEvent*)event
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];//点击屏幕收起键盘
}



- (void)refreshBaseControlValue {
    
    [self base_loadData];
    [self base_configNavigationBar];
    [self base_configSubViewStyles];
    [self base_configListViewStyles];
    [self base_masLayoutSubview];
}

/** 加载数据 **/
- (void)base_loadData {}

/** 配置导航条 **/
- (void)base_configNavigationBar {}

/** 配置子视图样式 **/
- (void)base_configSubViewStyles {}

/** 配置列表视图 **/
- (void)base_configListViewStyles {}

/** 布局子视图 **/
- (void)base_masLayoutSubview {}

#pragma mark -
#pragma mark - lazy load

- (UIButton *)leftButton
{
    if (!_leftButton) {
        _leftButton = [[UIButton alloc]init];
        [_leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_leftButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [_leftButton setExclusiveTouch:YES];
        [_leftButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:17.f];
        [_leftButton addTarget:self action:@selector(onLeftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _leftButton;
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [[UIButton alloc]init];
        [_rightButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [_rightButton setExclusiveTouch:YES];
        [_rightButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:17.f];
        
        [_rightButton addTarget:self action:@selector(onRightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _rightButton;
}





@end
